﻿using MaiPlanServer.Dto;
using MaiPlanServer.IServices.IMasterDataServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.MasterData
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaterialProductsManageController : ControllerBase
    {

        public MaterialProductsManageController(IMaterialsProductCServices mDb,IMaterialProductsManageServices Db)
        {
            MDb = mDb;
            this.Db = Db;
        }

        public IMaterialsProductCServices MDb { get; }
        public IMaterialProductsManageServices Db { get; }

        [HttpGet]
        public async Task<IActionResult> GetJson(int PageIndex, int PageSize, string mdName, string mdCode, string groupName)
        {
            try
            {
                var list = await Db.GetJsonAsync(PageIndex, PageSize, mdName, mdCode, groupName);
                var count = await Db.GetCountAsync(mdName, mdCode, groupName);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (System.Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }


        [HttpGet("GetTree")]
        public async Task<IActionResult> GetTreeJson()
        {
            try
            {
                List<MD_ITEM_TYPEDto> newList = new List<MD_ITEM_TYPEDto>();

                var list = await MDb.GetMdTypeAllAsync();
                newList = GetItem(list);

                return Ok(BaseState.CreateInstance(200, "请求成功", newList));
            }
            catch (System.Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        [HttpGet("GetUnitJson")]
        public async Task<IActionResult> GetUnitJson()
        {
            try
            {
                var list = await Db.GetUnitJsonAsync();

                return Ok(BaseState.CreateInstance(200, "请求成功", list));
            }
            catch (System.Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        [NonAction]
        private List<MD_ITEM_TYPEDto> GetItem(IEnumerable<MD_ITEM_TYPEDto> list)
        {
            List<MD_ITEM_TYPEDto> TreeList = new List<MD_ITEM_TYPEDto>();
            var parent = list.Where(x => string.IsNullOrEmpty(x.PARENT_TYPE_ID.ToString()));
            var lists = parent.ToList();
            var child = list.Where(x => !string.IsNullOrEmpty(x.PARENT_TYPE_ID.ToString()));
            var list111 = child.ToList();
            foreach (var item in parent)
            {
                //判断当前父元素有没有子元素
                var curr_child = child.Where(x => x.PARENT_TYPE_ID == item.ITEM_TYPE_ID);
                if (curr_child.Any())
                {
                    GetSubItem(item, list, curr_child);
                }
                TreeList.Add(item);
            }
            if (!parent.Any())
            {
                foreach (var item in child)
                {
                    TreeList.Add(item);
                }
            }
            return TreeList;
        }
        [NonAction]
        public void GetSubItem(MD_ITEM_TYPEDto parent, IEnumerable<MD_ITEM_TYPEDto> list, IEnumerable<MD_ITEM_TYPEDto> child)
        {
            foreach (var item in child)
            {
                var curr_child = list.Where(x => x.PARENT_TYPE_ID == item.ITEM_TYPE_ID);
                if (curr_child.Any())
                {
                    GetSubItem(item, list, curr_child);
                }

                parent.Children.Add(item);
            }
        }
    }
}
